package com.wx.rabbitmq.template;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wx.rabbitmq.entity.Order;
import com.wx.rabbitmq.entity.Packaged;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

import static com.wx.rabbitmq.config.RabbitMQConfig.*;

/**
 * @author wx
 * @Description:  RabbitTemplate操作使用
 * @date 2018/11/24 002422:11
 */
@RestController
public class RabbitTemplateController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * @description: RabbitTemplate发送消息测试1
     * @author: wx
     * @date: 2018/11/24 0024
     * @param
     * @return: java.lang.String
     */
    @PostMapping("/sendMessage")
    public String sendMessage(){
        //消息装饰器
        MessageProperties messageProperties = new MessageProperties();
        messageProperties.getHeaders().put("desc", "信息描述..");
        messageProperties.getHeaders().put("type", "自定义消息类型..");
        //创建消息 设置消息内容与消息装饰器
        Message message = new Message("Hello RabbitMQ".getBytes(), messageProperties);
        //进行消息发送
        rabbitTemplate.convertAndSend(TOPIC_1, "spring.amqp", message, new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                System.out.println("新增额外消息----");
                message.getMessageProperties().getHeaders().put("desc", "额外修改的信息描述");
                message.getMessageProperties().getHeaders().put("attr", "额外新加的属性");
                return message;
            }
        });
        return "发送成功";
    }


    /**
     * @description: RabbitTemplate发送消息2
     * @author: wx
     * @date: 2018/11/24 0024
     * @param
     * @return: java.lang.String
     */
    @PostMapping("/sendMessage_2")
    public String sendMessage_2(){
        //创建消息装饰器
        MessageProperties messageProperties = new MessageProperties();
        //设置发送消息类型
        messageProperties.setContentType("text/plain");
        //创建消息 设置消息内容与消息装饰器
        Message message = new Message("测试消息".getBytes(), messageProperties);
        //发送消息 形式-1 send消息必须是message对象
        rabbitTemplate.send(TOPIC_1, "spring.abc", message);
        //发送消息 形式-2   convertAndSend会自定绑定消息
        rabbitTemplate.convertAndSend(TOPIC_1, "spring.amqp", "hello routingkey=spring.amqp  message send!");
        rabbitTemplate.convertAndSend(TOPIC_2, "rabbit.abc", "hello routingkey=rabbit.abc  message send!");
        return "发送成功";
    }


    @PostMapping("/sendMessage_3")
    public String sendMessage_3(){
        //1 创建消息
        MessageProperties messageProperties = new MessageProperties();
        messageProperties.setContentType("text/plain");
        Message message = new Message("mq 消息1234".getBytes(), messageProperties);

        rabbitTemplate.send(TOPIC_1, "spring.abc", message);
        rabbitTemplate.send(TOPIC_2, "rabbit.abc", message);
        return "发送成功";
    }


    @PostMapping("/sendJsonMessage")
    public String sendJsonMessage() throws JsonProcessingException {
        Order order = new Order();
        order.setId("001");
        order.setName("消息订单");
        order.setContent("描述信息");
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(order);
        System.err.println("order to json: " + json);
        MessageProperties messageProperties = new MessageProperties();
        //这里注意一定要修改contentType为 application/json
        messageProperties.setContentType("application/json");
        Message message = new Message(json.getBytes(), messageProperties);
        rabbitTemplate.send(TOPIC_1, "spring.order", message);
        return "发送成功";
    }

    @PostMapping("/sendJavaMessage")
    public String sendJavaMessage() throws JsonProcessingException {
        Order order = new Order();
        order.setId("001");
        order.setName("订单消息");
        order.setContent("订单描述信息");
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(order);
        System.err.println("order to json: " + json);

        MessageProperties messageProperties = new MessageProperties();
        //这里注意一定要修改contentType为 application/json
        messageProperties.setContentType("application/json");
        //设置消息额外参数__TypeId__ 不然对象转换器无法把json串转换为对应的java对象
        messageProperties.getHeaders().put("__TypeId__", "com.wx.rabbitmq.entity.Order");
        Message message = new Message(json.getBytes(), messageProperties);
        rabbitTemplate.send(TOPIC_1, "spring.order", message);
        return "发送成功";
    }


    @PostMapping("/sendMappingMessage")
    public String sendMappingMessage() throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();

        Order order = new Order();
        order.setId("001");
        order.setName("订单消息");
        order.setContent("订单描述信息");

        String json1 = mapper.writeValueAsString(order);
        System.err.println("order to json: " + json1);

        MessageProperties messageProperties1 = new MessageProperties();
        //这里注意一定要修改contentType为 application/json
        messageProperties1.setContentType("application/json");
        messageProperties1.getHeaders().put("__TypeId__", "order");
        Message message1 = new Message(json1.getBytes(), messageProperties1);
        rabbitTemplate.send(TOPIC_1, "spring.order", message1);

        Packaged pack = new Packaged();
        pack.setId("002");
        pack.setName("包裹消息");
        pack.setDescription("包裹描述信息");

        String json2 = mapper.writeValueAsString(pack);
        System.err.println("pack to json: " + json2);

        MessageProperties messageProperties2 = new MessageProperties();
        //这里注意一定要修改contentType为 application/json
        messageProperties2.setContentType("application/json");
        messageProperties2.getHeaders().put("__TypeId__", "packaged");
        Message message2 = new Message(json2.getBytes(), messageProperties2);
        rabbitTemplate.send(TOPIC_1, "spring.pack", message2);
        return "发送成功";
    }


    @PostMapping("/sendImgaMessage")
    public String sendImgaMessage() throws IOException {
        byte[] body = Files.readAllBytes(Paths.get("d:", "QQ图片20181104190350.png"));
        MessageProperties messageProperties = new MessageProperties();
        messageProperties.setContentType("image/png");
        messageProperties.getHeaders().put("extName", "png");
        Message message = new Message(body, messageProperties);
        //不设置交换机默认采用队列名作为key路由到队列
        rabbitTemplate.send("", IMAGE_QUEUE, message);

        return "发送成功";
    }


    @PostMapping("/sendPDFMessage")
    public String sendPDFMessage() throws IOException {
        byte[] body = Files.readAllBytes(Paths.get("e:/书籍", "SpringMvc.pdf"));
        MessageProperties messageProperties = new MessageProperties();
        messageProperties.setContentType("application/pdf");
        Message message = new Message(body, messageProperties);
        //不设置交换机默认采用队列名作为key路由到队列
        rabbitTemplate.send("", PDF_QUEUE, message);

        return "发送成功";
    }


}
